package com.mdb.open.epay.resource;

import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.api.enums.ResultCode;
import com.mdb.bc.logger.LogAnnotation;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.bc.web.RepeatSubmitTypeEnum;
import com.mdb.bc.web.annotation.DenyRepeatSubmit;
import com.mdb.bc.web.support.SuperResources;
import com.mdb.open.epay.config.EPayMsgConstant;
import com.mdb.open.epay.domain.bo.SendPayOtpBo;
import com.mdb.open.epay.domain.bo.SendSignOtpBo;
import com.mdb.open.epay.domain.dto.epay.SendOtpDto;
import com.mdb.open.epay.enums.type.SmsBizTypeEnum;
import com.mdb.open.epay.facade.epay.SignFacade;
import com.mdb.open.epay.facade.epay.TradeFacade;
import com.mdb.open.epay.validator.OtpSendParamValidator;
import com.mdb.user.LoginUserInfoHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Otp
 *
 * @author liuyi
 * @date 2019-11-14
 */
@RestController
@RequestMapping("otp")
@Api(tags = "Otp")
public class OtpResource extends SuperResources {

    @Autowired
    private OtpSendParamValidator otpSendParamValidator;

    @Autowired
    private SignFacade signFacade;
    @Autowired
    private TradeFacade tradeFacade;

    @Autowired
    private LoginUserInfoHelper loginUserInfoHelper;

    @DenyRepeatSubmit(type = RepeatSubmitTypeEnum.EXCLUSIVE_SUBMIT)
    @LogAnnotation("獲取短信碼")
    @ApiOperation("獲取短信碼")
    @PostMapping("/send")
    public ResultEx sendOtp(@RequestBody SendOtpDto dto) {
        String customerCode = loginUserInfoHelper.getCustomerCode(true);
        if (SmsBizTypeEnum.SIGN.getValue().equals(dto.getSmsBizType().getValue())) {
            //發送簽約短信
            if (ObjectUtil.isAnyEmpty(dto.getBankCardNo(), dto.getCertNo(), dto.getCertType())) {
                return ResultEx.makeResult(ResultCode.INVALID_PARAMETER.getValue(), EPayMsgConstant.SEND_SIGN_OTP_PARAM_ERROR);
            }
            SendSignOtpBo signOtpBo = new SendSignOtpBo();
            signOtpBo.setCustomerCode(customerCode);
            signOtpBo.setBankCardNo(dto.getBankCardNo());
            signOtpBo.setPhoneArea(dto.getPhoneArea());
            signOtpBo.setPhoneNo(dto.getPhoneNo());
            signOtpBo.setCertType(dto.getCertType().getValue());
            signOtpBo.setCertNo(dto.getCertNo());
            return signFacade.sendSignOtp(signOtpBo);
        } else if(SmsBizTypeEnum.PAY.getValue().equals(dto.getSmsBizType().getValue())) {
            //發送支付短信
            if (ObjectUtil.isAnyEmpty(dto.getOrderNo(), dto.getBankSignNo())) {
                return ResultEx.makeResult(ResultCode.INVALID_PARAMETER.getValue(), EPayMsgConstant.SEND_PAY_OTP_PARAM_ERROR);
            }
            SendPayOtpBo payOtpBo = new SendPayOtpBo();
            payOtpBo.setCustomerCode(customerCode);
            payOtpBo.setPhoneArea(dto.getPhoneArea());
            payOtpBo.setPhoneNo(dto.getPhoneNo());
            payOtpBo.setBankSignNo(dto.getBankSignNo());
            payOtpBo.setOrderNo(dto.getOrderNo());
            return tradeFacade.sendPayOtp(payOtpBo);
        }
        return ResultEx.fail("未知類型");
    }

}
